#include "../../../head.h"

int main(int argc, char const *argv[])
{
    // 安全防护
    if (argc != 2)
    {
        printf("格式错误! ./%s file", argv[0]);
        return 0;
    }

    // 打开文件
    int fd = open(argv[1] , O_RDWR | O_CREAT , 0666);

    char buf[1024];
    char ch;
    memset(buf , 0 , sizeof(buf));
    int byte = 0;
    int word = 0;
    int line = 0;
    int i = 0;

    lseek(fd , 0 , SEEK_SET);
    while ((i = read(fd , &buf[byte] , 1)) > 0)
    {
        if (buf[byte] == ' ')
        {
            word++;
        }
        if (buf[byte] == '\n')
        {
            word++;
            line++;
        }
        byte++;
    }
    lseek(fd , 0 , SEEK_END);
    printf("文件%s中有%d个字符,%d个单词,%d行\n" , argv[1] , byte , word-1 , line);
    // 关闭文件
    close(fd);
    return 0;
}
